//
//  MGYTranslation.m
//  AT
//
//  Created by jiangjunwy on 16/7/27.
//  Copyright © 2016年 SanYouApp. All rights reserved.
//

#import "MGYTranslation.h"


@interface MGYTranslation ()

@property (assign, nonatomic) id context;
@property (strong, nonatomic) UIView* containerView;
@end

@implementation MGYTranslation

- (instancetype)initWithView:(UIImageView *)btnView
{
    if (self = [super init]) {
        
        NSAssert(btnView, @"btnView 不能为空");
        _animationView = btnView;
//        _originRect = btnView.frame;
    }
    return self;
}


- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.6;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    _context = transitionContext;
    self.containerView = [transitionContext containerView];
    
    if (self.reverse)
    {
        UIViewController* toVC = [_context viewControllerForKey:UITransitionContextToViewControllerKey];
        UIViewController* fromVC = [_context viewControllerForKey:UITransitionContextFromViewControllerKey];
        
        
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        toVC.view.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
        fromVC.view.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
        UIView * fffView = fromVC.view;
        
        [self.containerView addSubview:toVC.view];
        [self.containerView addSubview:fffView];
        //    fffView.layer.cornerRadius = screenSize.width/2;
        fffView.layer.masksToBounds = YES;
        //    toVC.view.alpha = 0;
        
        CATransform3D fina3D = CATransform3DMakeScale(1, 0.02, 0.2);
        [UIView animateWithDuration:.9 animations:^{
            
            fffView.layer.transform = fina3D;
            fffView.alpha = 0.3;
            //        CGSize ssss = CGSizeMake(1, 1);
            
        } completion:^(BOOL finished) {
            [fromVC.view removeFromSuperview];
            [_context completeTransition:YES];
        }];
    }else{
//        self.animationView = [[UIImageView alloc] initWithFrame:_changeView.frame];
//        self.animationView.image = _changeView.image;
        [self.containerView addSubview:self.animationView];
    }

}



- (void)startAnimation
{
    UIViewController* toVC = [_context viewControllerForKey:UITransitionContextToViewControllerKey];
//    UIViewController* fromVC = [_context viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    toVC.view.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);

    [self.containerView addSubview:toVC.view];

    toVC.view.alpha = 0;
    
//    CATransform3D fina3D = CATransform3DMakeScale(1, 0.2, 1);
    [UIView animateWithDuration:.7 animations:^{
        
        CGRect cR = self.animationView.frame;
        cR.origin.y = 0;
        self.animationView.frame = cR;

    } completion:^(BOOL finished) {
        
    }];
    
    [UIView animateWithDuration:1.2 animations:^{
        toVC.view.alpha = 1;
    } completion:^(BOOL finished) {
        [self.animationView removeFromSuperview];
        [_context completeTransition:YES];
    }];
}


@end
